//
//  ZtxqVC.m
//  AStage_shougongke
//
//  Created by 赵磊 on 16/1/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ZtxqVC.h"

@interface ZtxqVC ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSArray *dataArr;
@end

@implementation ZtxqVC
// 页面即将出现 将tabBar显示
- (void)viewWillAppear:(BOOL)animated
{
    // tabBar显示
    self.tabBarController.tabBar.hidden = YES;
}
// 页面即将消失 将tabBar隐藏
- (void)viewWillDisappear:(BOOL)animated
{
    // tabBar隐藏
    self.tabBarController.tabBar.hidden = NO;
}
- (void)dealloc
{
    // 拼接网址id
    [_zId release];
    // navigation的title
    [_timu release];
    [_collectionView release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.timu;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:234 / 255.0 green:86 / 255.0 blue:81 / 255.0 alpha:1.0];
    [self.navigationItem setHidesBackButton:YES];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 1) / 2 - 5, (WIDTH - 1) / 2);
    layout.sectionInset = UIEdgeInsetsMake(0.5, 0.5, 0.5, 0.5);
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[ZtxqCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 50, WIDTH, 50)];
    view.backgroundColor = [UIColor whiteColor];
    view.alpha = 0.8;
    [self.view addSubview:view];
    // 创建返回的button
    UIButton * fanhui = [UIButton buttonWithType:UIButtonTypeCustom];
    [fanhui setImage:[UIImage imageNamed:@"fanhui@2x"] forState:UIControlStateNormal];
    fanhui.frame = CGRectMake(0, 0, 50, 50);
    [fanhui addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:fanhui];
    [view release];
    [layout release];
    [_collectionView release];
    // 引用数据处理方法
    [self dataHandler];
}
// 数据处理
- (void)dataHandler
{
    [AFNModel GETWithURL:[NSString stringWithFormat:@"http://m.shougongke.com/index.php?&c=Course&a=topic&id=%@&vid=15", self.zId] completion:^(id result) {
        NSArray *arrTemp = result[@"data"][@"course"];
        self.dataArr = [ZtxqModel modelHandlerWithArray:arrTemp];
        [_collectionView reloadData];
    }];
}
// 返回
- (void)fanhui
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZtxqCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ZtxqModel *m = _dataArr[indexPath.item];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.thumb]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JiaochengxiangqingVC *ztxqVC = [[JiaochengxiangqingVC alloc] init];
    ZtxqModel *m = _dataArr[indexPath.item];
    ztxqVC.hand_id = m.hand_id;
    [self.navigationController pushViewController:ztxqVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
